import { Context } from '../types'

export default class BaseService {
  public static async query(ctx: Context, params) {
    const { offset, addtions, order = 'DESC' } = params
    return ctx.model.findAndCountAll({
      where: addtions ?? {},
      offset: offset ? offset - 1 : 0,
    })
  }

  public static async delete(ctx: Context, params) {
    if (!params.id) return null
    return ctx.model.destroy({
      where: {
        id: params.id,
        user_id: ctx.user_id,
      },
    })
  }

  public static async update(ctx: Context, params) {
    const { id, ...updated } = params
    if (!id) return null
    return ctx.model.update(updated, {
      where: {
        id,
        user_id: ctx.user_id,
      },
    })
  }

  public static async create(ctx: Context, params) {
    return ctx.model.create(params)
  }
}
